package org.summerframework.component.security.spring;

import org.summerframework.component.security.permissions.service.UserPermissionsService;
import org.summerframework.component.security.role.service.UserRoleService;
import org.summerframework.component.security.spring.access.RoleAnnotationSecurityMetadataSourceHandler;

import javax.annotation.Resource;

/**
 * @author 石超
 * @version v1.0.0
 */
public class DefaultRoleAnnotationSecurityMetadataSourceHandler implements RoleAnnotationSecurityMetadataSourceHandler {

    @Resource
    private UserRoleService userRoleService;

    @Resource
    private UserPermissionsService userPermissionsService;

    @Override
    public boolean hasPermission(String code) {
        try {
            return userPermissionsService.selectByCode(code) != null;
        } catch (Exception e) {
            return false;
        }
    }

    @Override
    public boolean hasRole(String code) {
        try {
            return userRoleService.selectByCode(code) != null;
        } catch (Exception e) {
            return false;
        }
    }
}
